package com.android.bookgarden.utli;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.annotation.NonNull;

import java.util.List;

/**
 * 处理图片和拍照的
 */
public class IntentUtil {
    public static Intent getIntentOfTakingPhoto(@NonNull Context context, @NonNull Uri photoUri) {
        Intent takingPhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        takingPhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        grantIntentAccessUriPermission(context, takingPhotoIntent, photoUri);
        return takingPhotoIntent;
    }

    public static Intent getIntentOfPickingPicture() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        return intent;
    }

    public static Intent getIntentOfCroppingImage(@NonNull Context context, @NonNull Uri imageUri) {
        Intent croppingImageIntent = new Intent("com.android.camera.action.CROP");
        croppingImageIntent.setDataAndType(imageUri, "image/*");
        croppingImageIntent.putExtra("crop", "true");
        //crop into circle image
//        croppingImageIntent.putExtra("circleCrop", "true");
        //The proportion of the crop box is 1:1
        croppingImageIntent.putExtra("aspectX", 1);
        croppingImageIntent.putExtra("aspectY", 1);
        //Crop the output image size
        croppingImageIntent.putExtra("outputX", 256);//输出的最终图片文件的尺寸, 单位是pixel
        croppingImageIntent.putExtra("outputY", 256);
        //scale selected content
        croppingImageIntent.putExtra("scale", true);
        //image type
        croppingImageIntent.putExtra("outputFormat", "JPEG");
        croppingImageIntent.putExtra("noFaceDetection", true);
        //false - don't return uri |  true - return uri
        croppingImageIntent.putExtra("return-data", true);//
        croppingImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        grantIntentAccessUriPermission(context, croppingImageIntent, imageUri);
        return croppingImageIntent;
    }

    private static void grantIntentAccessUriPermission(@NonNull Context context, @NonNull Intent intent, @NonNull Uri uri) {
        if (!UriUtil.requireSDKInt(Build.VERSION_CODES.N)) {//in pre-N devices, manually grant uri permission.
            List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
            for (ResolveInfo resolveInfo : resInfoList) {
                String packageName = resolveInfo.activityInfo.packageName;
                context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
        } else {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }
    }
}
